package com.cssy.yd.logic.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.yd.logic.dao.LogicWhCheckMissionDAO;
import com.cssy.yd.logic.dao.LogicWhCheckMissionItemDAO;
import com.cssy.yd.logic.entity.LogicWhAffDO;
import com.cssy.yd.logic.entity.LogicWhCheckMissionDO;
import com.cssy.yd.logic.entity.LogicWhCheckMissionItemDO;
import com.cssy.yd.logic.service.ILogicWhAffStockService;
import com.cssy.yd.logic.service.ILogicWhCheckMissionService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.exception.SyCodeException;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("logicWhCheckMissionService")
public class LogicWhCheckMissionServiceImpl implements ILogicWhCheckMissionService{
	@Resource(name = "logicWhCheckMissionDAO")
	private LogicWhCheckMissionDAO logicWhCheckMissionDAO;
	
	@Resource(name = "logicWhCheckMissionItemDAO")
	private LogicWhCheckMissionItemDAO logicWhCheckMissionItemDAO;

	@Resource(name = "logicWhAffStockService")
	private ILogicWhAffStockService logicWhAffStockService;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicWhCheckMissionDO>  list=logicWhCheckMissionDAO.find(scs);
		rs.setRecordCount(logicWhCheckMissionDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		Integer i =logicWhCheckMissionDAO.deleteLogicWhCheckMissionDOByPrimaryKey(id);
		return i;
	}

	@Override
	public Integer update(LogicWhCheckMissionDO t, User user) throws SyBaseException {
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		return logicWhCheckMissionDAO.updateLogicWhCheckMissionDO(t);		
	}

	@Override
	public LogicWhCheckMissionDO save(LogicWhCheckMissionDO t, User user) throws SyBaseException {
		t.setDcruser(user.getName());
		t.setDctime(new Date());
		t.setId(IDCodeTypeUtil.getId());
		logicWhCheckMissionDAO.insertLogicWhCheckMissionDO(t);
		return t;
	}

	@Override
	public LogicWhCheckMissionDO findListById(String id) throws SyBaseException {
		return logicWhCheckMissionDAO.findLogicWhCheckMissionDOByPrimaryKey(id);
	}
	
	@Override
	public SearchResult findItemListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicWhCheckMissionItemDO>  list=logicWhCheckMissionItemDAO.find(scs);
		rs.setRecordCount(logicWhCheckMissionItemDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer deleteItem(String id) throws SyBaseException {
		Integer i =logicWhCheckMissionItemDAO.deleteLogicWhCheckMissionItemDOByPrimaryKey(id);
		return i;
	}

	@Override
	public Integer updateItem(LogicWhCheckMissionItemDO t) throws SyBaseException {
		return logicWhCheckMissionItemDAO.updateLogicWhCheckMissionItemDO(t);
	}

	@Override
	public LogicWhCheckMissionItemDO saveItem(LogicWhCheckMissionItemDO t) throws SyBaseException {
		t.setId(IDCodeTypeUtil.getId());
		logicWhCheckMissionItemDAO.insertLogicWhCheckMissionItemDO(t);
		return t;
	}

	@Override
	public LogicWhCheckMissionItemDO findItemListById(String id) throws SyBaseException {
		return logicWhCheckMissionItemDAO.findLogicWhCheckMissionItemDOByPrimaryKey(id);
	}

	@Override
	public List<LogicWhCheckMissionItemDO> findItemListByCheckMissionID(String id)
			throws SyBaseException {
		return logicWhCheckMissionItemDAO.findItemByCheckMissionID(id);
	}

	@Override
	public void checksave(HttpServletRequest request, LogicWhCheckMissionDO curAP)
			throws SyBaseException, SyCodeException {
		try {
			String checkdata = request.getParameter("checkdata");
			List<LogicWhCheckMissionItemDO> axxlist = JSONArray.parseArray(checkdata,LogicWhCheckMissionItemDO.class);
			LogicWhCheckMissionDO lcm = logicWhCheckMissionDAO.findLogicWhCheckMissionDOByPrimaryKey(curAP.getId());
			int rst=0;
			if(curAP.getBillStatus()==0) {//新增
				String id = IDCodeTypeUtil.getId();
				curAP.setCheckMissionCode(syCodeService.getCode("PM","PM",SyCode.DATE_YYMM,5));
				curAP.setPdCount(0);
				curAP.setDcruser(SessionUtil.getUser(request).getName());
				curAP.setDctime(new Date());
				curAP.setId(id);
				logicWhCheckMissionDAO.insertLogicWhCheckMissionDO(curAP);
				for (int i = 0; i < axxlist.size(); i++) {
					axxlist.get(i).setCheckMissionID(id);
					axxlist.get(i).setId(IDCodeTypeUtil.getId());
					logicWhCheckMissionItemDAO.insertLogicWhCheckMissionItemDO(axxlist.get(i));
				}
			}
			if (curAP.getBillStatus()==1) {//提交
				logicWhCheckMissionDAO.updateLogicWhCheckMissionDO(curAP);
				logicWhCheckMissionItemDAO.deleteItemByCheckMissionID(curAP.getId());
				for (LogicWhCheckMissionItemDO cmi : axxlist){
					String id = IDCodeTypeUtil.getId();
					cmi.setId(id);
					cmi.setCheckMissionID(curAP.getId());
					logicWhCheckMissionItemDAO.insertLogicWhCheckMissionItemDO(cmi);
				}
			}
			if(curAP.getBillStatus()==3){  //提交审核
				if(lcm.getBillStatus()==2){
					lcm.setVersion(curAP.getVersion());
					lcm.setBillStatus(curAP.getBillStatus());
					this.update(lcm, SessionUtil.getUser(request));
				}else{
					throw new SyBaseException("当前单据未在您提交权限内！");
				}
			}
			if(curAP.getBillStatus()==4){//审核通过(结束盘点任务）
				if(lcm.getBillStatus()==3){
					if(lcm.getPdCount()>0){
						lcm.setVersion(curAP.getVersion());
						lcm.setBillStatus(curAP.getBillStatus());
						rst = this.update(lcm, SessionUtil.getUser(request));
						if(1==rst){
							if(axxlist.size() > 0){
								for(LogicWhCheckMissionItemDO ci : axxlist){
									LogicWhAffDO lwhaff=new LogicWhAffDO();
									lwhaff.setBillCode(lcm.getCheckMissionCode());
									lwhaff.setBillType("PD");
									lwhaff.setWhCode(lcm.getWhCode());
									lwhaff.setWhname(lcm.getWhName());
									lwhaff.setCode(ci.getCode());
									lwhaff.setName(ci.getName());
									lwhaff.setSpec(ci.getSpec());
									lwhaff.setBatch(ci.getBatch());
									lwhaff.setJunit(ci.getUnit());
									lwhaff.setUnit(ci.getUnit());
									lwhaff.setDcruser(SessionUtil.getUser(request).getName());
									lwhaff.setDctime(new Date());
									for(int i=1;i<5;i++){
										lwhaff.setStatus(String.valueOf(i));
										if(i==1&&Math.abs(ci.getYdengQuantityCY())>0){
											lwhaff.setJquantity(ci.getYdengQuantityCY());
											logicWhAffStockService.updateWhAffstockQuantity(lwhaff, SessionUtil.getUser(request),false);
										}else if(i==2&&Math.abs(ci.getCipinQuantityCY())>0){
											lwhaff.setJquantity(ci.getCipinQuantityCY());
											logicWhAffStockService.updateWhAffstockQuantity(lwhaff, SessionUtil.getUser(request),false);
										}else if(i==3&&Math.abs(ci.getJiupinQuantityCY())>0){
											logicWhAffStockService.updateWhAffstockQuantity(lwhaff, SessionUtil.getUser(request),false);
											lwhaff.setJquantity(ci.getJiupinQuantityCY());
										}else if(i==4&&Math.abs(ci.getFreezeQuantityCY())>0){
											logicWhAffStockService.updateWhAffstockQuantity(lwhaff, SessionUtil.getUser(request),false);
											lwhaff.setJquantity(ci.getFreezeQuantityCY());
										}
										
									}
								}
							}
						}else{
							throw new SyBaseException("您修改的当前行数据已发生改变,请重新点开查看");
						}
					}else{
						throw new SyBaseException("当前任务还未盘点过！");
					}
				}else{
					throw new SyBaseException("当前单据未在您审核权限内！");
				}
			}
			if(curAP.getBillStatus()==5){  //驳回重盘
				if(lcm.getBillStatus()==3){
					lcm.setVersion(curAP.getVersion());
					lcm.setBillStatus(curAP.getBillStatus());
					this.update(lcm, SessionUtil.getUser(request));
				}else{
					throw new SyBaseException("当前单据未在您审核权限内！");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

}
